<?php
defined('IN_SITE') or exit('Access Denied');
define('MOD_ROOT', WWW_ROOT.''.$module.'/');

#require MOD_ROOT.'include/global.func.php';

if(!class_exists('member'))
{
	require WWW_ROOT.'member/member.class.php';
}
if(!is_a($member, 'member'))
{
	$member = new member();
}

if(!class_exists(sendmail))
{
	//$mail = load('sendmail.class.php');
	require_once 'sendmail.class.php';
	$mail = new sendmail();
}

require_once MOD_ROOT.'admin/admin.class.php';
$admin = new admin();

require 'include/message_admin.class.php';
$message_admin = new message_admin();


include_once('message.lang.php');
include_once('message_admin.lang.php');

//if($forward) $forward = HTTP_REFERER;
switch ($action) 
{
	case 'manage':
		if ($dosubmit)
		{
			if(!$del_msgid) showmessage('请选择短消息');
			if(!$message_admin->delete($del_msgid))
			{
				showmessage($message->msg(), $forward);
			}
			showmessage($LANG['operation_success'], $forward);
		}
		else
		{
			$where = '';
			if($username)
			{
				if(!class_exists('member'))
				{
					require WWW_ROOT.'member/include/member.class.php';
				}
				$member = new member();
				$userid = $member->get_userid($username);
				$where  .= $where ?  " AND send_from_id='$userid'" : " send_from_id='$userid'";
			}
			if($subject)
			{
				$where .= $where ? " AND subject LIKE '%$subject%'" : "subject LIKE '%$subject%'";
			}
			if($datestart) 
			{
				$start_time = strtotime($datestart);
				$where .= $where ? " AND $start_time <= message_time" : " $start_time <= message_time";
			}
			if ($dateend)
			{
				$end_time = strtotime($dateend);
				$where .= $where ? " AND message_time <= $end_time": " message_time <= $end_time";;
			}
			$arr_message = array();
			$where .= $where ? " AND replyid=0" : " replyid=0";
			if(!$order) $order = "messageid ASC";
			$page = max(intval($page), 1);
			$pagesize = $PHPCMS['pagesize'] ? $PHPCMS['pagesize'] : 30;
			$arr_message = $message_admin->list_message($where, $order, $page, $pagesize);
			$message_num = $message_admin->count_message($where);
			$pages = pages($message_num, $page, $pagesize);
			include 'templates/manage.tpl.php';
		}
		break;
	case 'delete':
		if($dosubmit)
		{
			if(!$message_admin->del_by_time($time))
			{
				showmessage($message_admin->msg(), $forward);
			}	
			showmessage($LANG['operation_success'], $forward);
		}
		else 
		{
			$stat = load('stat.class.php');
			$del_from_array = array(0=>'选择删除条件',7=>'一周前',30=>'一个月前',1=>'全部');
			$num_inbox = $message_admin->num_inbox();
			$num_outbox = $message_admin->num_outbox();
			include 'templates/delete_msg.tpl.php';
		}
		break;
		
	case 'priv':
		if($dosubmit)
		{
			$priv_role->update('module', $module, $priv_roleid);
			showmessage($LANG['save_setting_success'], $forward);
		}
		else
		{
			$privs = include 'include/priv.inc.php';
			include 'templates/priv.tpl.php';
		}
	break;
	
	case 'sendmessage':
		if ($dosubmit)
		{
			if(!$pagesize) $pagesize = $sendnumber ? $sendnumber: $PHPCMS['pagesize'];
			$page = max(intval($page), 1);
			$offset = $pagesize*($page-1);
			
			/*按用户组*/
			if($type == 'group')
			{
				if(!$groupid) showmessage('请选择用户组');
				if($groupid == 'all' && !$total)
				{
					$total = $member->count_member();
				}
				elseif(!$total && $groupid != 'all')
				{
					$total = $member->count_member(" groupid='$groupid'");
				}
				$pages = ceil($total/$pagesize);
				$arr_user = ($groupid == 'all') ? $member->get_all($order, $page, $pagesize) : $member->get_by_groupid($groupid, $order, $page, $pagesize);
				if(!$arr_user) showmessage('该用户组没有会员');
				foreach ($arr_user as $touser)
				{
					$touserid = $touser['userid'];
					if(!$message_admin->send_new($touserid, $_userid, $subject, $content))
					{
						showmessage($message_admin->msg(), $forward);
					}
					if($send) $mail->send($touser['email'], $subject, $content/*, $_email*/);
					$memberinfo = array('userid'=>$touserid, 'message'=>1);
					$member->edit($memberinfo);
				}	
				if($pages > $page)
				{
					$page++;
					$forward = "?module=$module&file=$file&action=$action&name=$name&groupid=$groupid&dosubmit=1&subject=$subject&content=$content&&page=$page&total=$total&pagesize=$pagesize&type=group";
					showmessage('开始发送短消息', $forward);
				}
				else
				{
					$forward = "?module=$module&file=$file&action=manage";
					showmessage($LANG['message_send_successful'], $forward);
				}
			}
			/*按角色*/
			elseif($type == 'role')
			{
				$total = $admin->count_admin(" roleid='$roleid'");
				$pages = ceil($total/$pagesize);
				$arr_user = $admin->listadmin(" roleid='$roleid'", $order = '', $page, $pagesize);
				if(!$arr_user) showmessage('该管理角色没有会员');
				foreach ($arr_user as $touser)
				{
					$touserid = $touser['userid'];
					if(!$message_admin->send_new($touserid, $_userid, $subject, $content))
					{
						showmessage($message_admin->msg(), $forward);
					}
					$memberinfo = array('userid'=>$touserid, 'message'=>1);
					$member->edit($memberinfo);
					
					if($send) $mail->send($touser['email'], $subject, $content/*, $_email*/);
				}
				
				if($pages > $page)
				{
					$page++;
					$forward = "?module=$module&file=$file&action=$action&name=$name&roleid=$roleid&dosubmit=1&subject=$subject&content=$content&&page=$page&total=$total&type=role";
					showmessage('开始发送短消息', $forward);
				}
				else
				{ 
					showmessage($LANG['message_send_successful'], "?module=$module&file=$file&action=manage");
				}
			}
		}
		else
		{
			$GROUP[0] = '请选择';
			$GROUP['all'] = '所有用户';		
			ksort($GROUP);
			$role_group = cache_read('role.php');
			if(!isset($type)) $type = 'group';
			include 'templates/sendmessage.tpl.php';
		}
	break;
	
	default:
		include 'templates/manage.tpl.php';
		break;
}
?>